package com.eric.satoken;

import cn.dev33.satoken.stp.StpInterface;
import com.eric.entity.Permission;
import com.eric.entity.Roles;
import com.eric.entity.Roles_Permission;
import com.eric.entity.User_Roles;
import com.eric.service.GetUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class StpInterfaceImpl implements StpInterface {
    @Autowired
    private GetUserInfoService getUserInfoService;

    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        List<String> permissionList=new ArrayList<>();
//        把传进来的参数转换为integer类型，loginId就是StpUtil.login（）方法登录时的登录id
//        这个id与user_id一致（登录的controller类，使用前端传来的user_id登录）
        Integer i = Integer.parseInt(loginId.toString());
//      getUserInfo对象根据user_id获取到User_Roles集合对象
        List<User_Roles> rolesList = getUserInfoService.getRolesListByUserId(i);
//        获取到roleId
        Integer roleId = rolesList.get(0).getRole_id();
//        根据role_id获取Roles_Permission对象
        List<Roles_Permission> rolesPermissions = getUserInfoService.getByRole_id(roleId);
        rolesPermissions.forEach(rolesPermission -> {
            rolesPermission.getPermissionList().forEach(permission -> {
                permissionList.add(permission.getP_name());
            });
        });
        return permissionList;
    }

    @Override
//    获取角色列表
    public List<String> getRoleList(Object loginId, String loginType) {
        List<String> roleList=new ArrayList<>();
        Integer i = Integer.parseInt(loginId.toString());
        List<User_Roles> rolesListByUserId = getUserInfoService.getRolesListByUserId(i);
        rolesListByUserId.forEach(userRoles -> {
            userRoles.getRoles().forEach(role -> {
                roleList.add(role.getRole_name());
            });
        });
        return roleList;
    }
}
